<?php

namespace Yolo\Components\Scheduler;

use Yolo\ComponentManager\AbstractComponent;
use Yolo\Components\Scheduler\Observers\AfterWorkerStartedObserver;
use Yolo\Components\Validator\ValidatorFactory;
use Yolo\Di\DI;
use Yolo\Observers\ObserverHelper;

class SchedulerComponent extends AbstractComponent
{
    public function __construct(
        private readonly ValidatorFactory $validatorFactory,
    ){}

    /**
     * Register the component.
     * @return void
     */
    public function register(): void
    {

    }

    public function registered(): void
    {
        // 注册计划任务配置的验证规则。
        $this->validatorFactory->registryValidationRules('schedulerConfigValidation', [
            'class' => 'required|string',
            'interval' => 'sometimes|integer',
            'every' => 'sometimes|array',
            'runRightAway' => 'required|boolean',
        ]);

        $observerHelper = DI::use(ObserverHelper::class);
        $observerHelper->subscribe(
            AfterWorkerStartedObserver::class
        );
    }
}
